package ltd.hxya.novel.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ltd.hxya.novel.book.dto.HomeBookRespDto;
import ltd.hxya.novel.book.entity.BookInfo;
import ltd.hxya.novel.book.entity.HomeBook;
import ltd.hxya.novel.book.mapper.HomeBookMapper;
import ltd.hxya.novel.book.service.BookInfoService;
import ltd.hxya.novel.book.service.HomeBookService;
import ltd.hxya.novel.book.utils.constant.DatabaseConsts;
import ltd.hxya.novel.book.utils.resp.RestResp;
import ltd.hxya.novel.common.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 首页的Home的处理过程
 * @author hxya
 * @since 2022-09-13
 */
@Service
public class HomeBookServiceImpl extends ServiceImpl<HomeBookMapper, HomeBook> implements HomeBookService {

    @Autowired
    private BookInfoService bookInfoService;

    @Override
    public Result<List<HomeBookRespDto>> listHomeBooks() {

        // 从首页小说推荐表中查询出需要推荐的小说
        QueryWrapper<HomeBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc(DatabaseConsts.CommonColumnEnum.SORT.getName());
        List<HomeBook> homeBooks = baseMapper.selectList(queryWrapper);

        // 获取推荐小说ID列表
        if (!CollectionUtils.isEmpty(homeBooks)) {
            List<String> bookIds = homeBooks.stream()
                    .map(HomeBook::getBookId)
                    .collect(Collectors.toList());

            // 根据小说ID列表查询相关的小说信息列表
            QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
            bookInfoQueryWrapper.in(DatabaseConsts.CommonColumnEnum.ID.getName(), bookIds);
            List<BookInfo> bookInfos = bookInfoService.getbookList(bookInfoQueryWrapper);
            // 组装 HomeBookRespDto 列表数据并返回
            if (!CollectionUtils.isEmpty(bookInfos)) {
                // bookInfos改成map的方式
                Map<String, BookInfo> bookInfoMap = bookInfos.stream()
                        .collect(Collectors.toMap(BookInfo::getId, Function.identity()));

                Result<List<HomeBookRespDto>> listRestResp = Result.success(homeBooks.stream().filter(book->{
                    BookInfo bookInfo = bookInfoMap.get(book.getBookId());
                    if (ObjectUtils.isEmpty(bookInfo)){
                        return false;
                    }
                    return true;
                }).map(book -> {
                    BookInfo bookInfo = bookInfoMap.get(book.getBookId());
                    HomeBookRespDto bookRespDto = new HomeBookRespDto();
                    bookRespDto.setType(book.getType());
                    bookRespDto.setBookId(book.getBookId());
                    bookRespDto.setBookName(bookInfo.getBookName());
                    bookRespDto.setPicUrl(bookInfo.getPicUrl());
                    bookRespDto.setAuthorName(bookInfo.getAuthorName());
                    bookRespDto.setBookDesc(bookInfo.getBookDesc());
                    return bookRespDto;
                }).collect(Collectors.toList()));
                System.out.println(listRestResp);
                return listRestResp;
            }
        }

        return Result.success(null);
    }
}
